//
//  RightViewController.m
//  Journey
//
//  Created by dllo on 16/5/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LeftViewController.h"
#import "CollectViewController.h"

static NSString *const leftcell = @"leftcell";
@interface LeftViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *leftTableView;
@property(nonatomic,copy)NSMutableArray *leftArray;
@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.leftTableView= [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 150 * WIDTHC, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.leftTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.leftTableView];
    
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    
    self.leftVCArray = [NSMutableArray arrayWithObjects:@"我的收藏",@"清除缓存",nil];
    
    [self.leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:leftcell];
    
    
    
    
    
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

//cell的显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:leftcell];
    
    cell.textLabel.text = [self.leftVCArray objectAtIndex:indexPath.row];
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFFFF,0x363636);
    return cell;
    
}

//cell点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CollectViewController *collect = [[CollectViewController alloc] init];
        UINavigationController *collectNa = [[UINavigationController alloc] initWithRootViewController:collect];
        
        [self presentViewController:collectNa animated:YES completion:^{
            
        }];
    }
    
     if (indexPath.row == 1) {
         UIAlertController *alerAction = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"确定删除%.2fM缓存吗?",[self clearCache]] message:nil preferredStyle:UIAlertControllerStyleAlert];
         [alerAction addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
             
         }]];
         [alerAction addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
             
             [self removedChace];
             [self.leftTableView reloadData];
             
         }]];
         
         [self presentViewController:alerAction animated:YES completion:nil];
     }
    
}

//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

//找到缓存路径,读取缓存
-(float)clearCache{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    float folderSize;
    
    if ([fileManager fileExistsAtPath:path]) {
        //拿到有文件的数组
        NSArray *childerFile = [fileManager subpathsAtPath:path];
        //拿到每个文件的名字,如果有不想清除的文件,就在这里判断
        for (NSString *fileName in childerFile) {
            //将路径拼接到一起
            NSString *fullPath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizePath:fullPath];
        }
    }
    return folderSize;
}

//缓存的计算
-(float)fileSizePath:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

//清除缓存
-(void)removedChace{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如果有需要,加入条件过滤掉不想删除的文件
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
